import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
import seaborn as sns

# 导入数据集
data = pd.read_csv('dataset_sdn.csv')

# 数据标签分析
label_dict = dict(data.label.value_counts())
sns.countplot(x='label',data=data,hue=data['label'])
print(data.columns)
print(data.label.value_counts())
plt.title("The number of Benign and Maliciuos Requests in dataset")
plt.show()

labels = ["Maliciuous", 'Benign']
sizes = [dict(data.label.value_counts())[0], dict(data.label.value_counts())[1]]
plt.figure(figsize=(13, 8))
plt.pie(sizes, labels=labels, autopct='%1.1f%%',
        shadow=True, startangle=90)
plt.legend(["Maliciuous", "Benign"])
# plt.title('数据集中正常和攻击请求的百分比')
plt.title('The percentage of Benign and Maliciuos Requests in dataset')
plt.show()

# 特征空值情况
figure(figsize=(9, 5), dpi=80)
data[data.columns[data.isna().sum() >= 0]].isna().sum().sort_values().plot.bar()
plt.title("Features which has NuLL values")
# Features which has NuLL values
plt.show()
print(data.isnull().sum())

#### 数字值的特征和对象特征

numeric_df = data.select_dtypes(include=['int64', 'float64'])
object_df = data.select_dtypes(include=['object'])
numeric_cols = numeric_df.columns
object_cols = object_df.columns
print('Numeric Columns: ')
print(numeric_cols, '\n')
print('Object Columns: ')
print(object_cols, '\n')
print('Number of Numeric Features: ', len(numeric_cols))
print('Number of Object Features: ', len(object_cols))

figure(figsize=(12, 7), dpi=80)
plt.barh(list(dict(data.src.value_counts()).keys()), dict(data.src.value_counts()).values(), color='lawngreen')

for idx, val in enumerate(dict(data.src.value_counts()).values()):
    plt.text(x=val, y=idx - 0.2, s=str(val), color='r', size=13)

plt.xlabel('Number of Requests')
plt.ylabel('IP addres of sender')
plt.title('Number of all reqests')
plt.show()

figure(figsize=(12, 7), dpi=80)
plt.barh(list(dict(data[data.label == 1].src.value_counts()).keys()),
         dict(data[data.label == 1].src.value_counts()).values(), color='blue')

for idx, val in enumerate(dict(data[data.label == 1].src.value_counts()).values()):
    plt.text(x=val, y=idx - 0.2, s=str(val), color='r', size=13)

plt.xlabel('Number of Requests')
plt.ylabel('IP addres of sender')
plt.title('Number of Attack requests')
plt.show()

figure(figsize=(12, 7), dpi=80)
plt.barh(list(dict(data.src.value_counts()).keys()), dict(data.src.value_counts()).values(), color='lawngreen')
plt.barh(list(dict(data[data.label == 1].src.value_counts()).keys()),
         dict(data[data.label == 1].src.value_counts()).values(), color='blue')

for idx, val in enumerate(dict(data.src.value_counts()).values()):
    plt.text(x=val, y=idx - 0.2, s=str(val), color='r', size=13)

for idx, val in enumerate(dict(data[data.label == 1].src.value_counts()).values()):
    plt.text(x=val, y=idx - 0.2, s=str(val), color='w', size=13)

plt.xlabel('Number of Requests')
plt.ylabel('IP addres of sender')
plt.legend(['All', 'malicious'])
plt.title('Number of requests from different IP adress')
plt.show()

figure(figsize=(10, 6), dpi=80)
plt.bar(list(dict(data.Protocol.value_counts()).keys()), dict(data.Protocol.value_counts()).values(), color='r')
plt.bar(list(dict(data[data.label == 1].Protocol.value_counts()).keys()),
        dict(data[data.label == 1].Protocol.value_counts()).values(), color='b')

plt.text(x=0 - 0.15, y=41321 + 200, s=str(41321), color='black', size=17)
plt.text(x=1 - 0.15, y=33588 + 200, s=str(33588), color='black', size=17)
plt.text(x=2 - 0.15, y=29436 + 200, s=str(29436), color='black', size=17)

plt.text(x=0 - 0.15, y=9419 + 200, s=str(9419), color='w', size=17)
plt.text(x=1 - 0.15, y=17499 + 200, s=str(17499), color='w', size=17)
plt.text(x=2 - 0.15, y=13866 + 200, s=str(13866), color='w', size=17)

plt.xlabel('Protocol')
plt.ylabel('Count')
plt.legend(['All', 'malicious'])
plt.title('The number of requests from different protocols')
plt.show()

df = data.copy()

figure(figsize=(8, 4), dpi=80)
plt.hist(df.dur, bins=20, color='b')
plt.title('Duration')
plt.show()

figure(figsize=(8, 4), dpi=80)
plt.hist(df.tx_bytes, bins=20, color='r')
plt.title('TX_BYTES - Transmitted Bytes')
plt.show()

figure(figsize=(8, 4), dpi=80)
plt.hist(df.tx_kbps, bins=10, color='g')
plt.title('TX_KBPC')
plt.show()

plt.hist(df.switch, bins=20, color='r')
plt.title('SWITCH')
plt.xlabel('SWITCH')
plt.show()

plt.hist(df[df['label'] == 1].switch, bins=20, color='r')
plt.title('SWITCH')
plt.xlabel('SWITCH')
plt.show()